package cn.shujuhai.common.service.impl;

import cn.shujuhai.common.domain.AccessLogs;
import cn.shujuhai.common.domain.Setting;
import cn.shujuhai.common.domain.SysOperationLog;
import cn.shujuhai.common.mapper.SettingMapper;
import cn.shujuhai.common.mapper.SysOperationLogMapper;
import cn.shujuhai.common.service.AccessLogsService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * 实现 AccessLogsService 接口的服务层类
 * 提供对访问日志进行数据库操作的具体实现
 */
@Service
@RequiredArgsConstructor
public class AccessLogsServiceImpl implements AccessLogsService {

    private final SysOperationLogMapper sysOperationLogMapper;
    private final SettingMapper settingMapper;

    /**
     * 获取总的访问次数
     *
     * @return 总的访问次数
     */
    @Override
    public int getTotalVisitCount() {
        try {
            // 查询数据库中所有唯一 login_ip 地址的数量
            QueryWrapper<SysOperationLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("COUNT(DISTINCT ip_address) as unique_visits");
            Map<String, Object> result = sysOperationLogMapper.selectMaps(queryWrapper).get(0);
            Long uniqueVisits = (Long) result.get("unique_visits");

            // 从 sys_setting 表中获取 oldTotal 字段的值
            QueryWrapper<Setting> settingQueryWrapper = new QueryWrapper<>();
            settingQueryWrapper.eq("setting_key", "OLD_TOTAL"); // 假设 setting_name 字段用于标识 oldTotal
            Setting setting = settingMapper.selectOne(settingQueryWrapper);
            int oldTotal = (setting != null && setting.getSettingValue() != null) ? Integer.parseInt(setting.getSettingValue()) : 0;

            // 将两个值相加
            return (uniqueVisits != null ? uniqueVisits.intValue() : 0) + oldTotal;
        } catch (Exception e) {
            // 打印堆栈信息并抛出运行时异常
            e.printStackTrace();
            throw new RuntimeException("查询数据库异常", e);
        }
    }

    // 以下方法未实现，返回默认值或空对象
    @Override
    public boolean saveBatch(Collection<AccessLogs> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<AccessLogs> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<AccessLogs> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(AccessLogs entity) {
        return false;
    }

    @Override
    public AccessLogs getOne(Wrapper<AccessLogs> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<AccessLogs> getOneOpt(Wrapper<AccessLogs> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<AccessLogs> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<AccessLogs> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<AccessLogs> getBaseMapper() {
        return null;
    }

    @Override
    public Class<AccessLogs> getEntityClass() {
        return null;
    }
}
